﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KingSite.Shop.Core.Service;
using KingSite.Shop.Core.Service.Model;

namespace KingSite.Shop.Service {
    public class ParserUrlParamater :IParserUrlParamater {
        public GoodsListUrlParamater Parser(string para) {
            // 标准的参数格式：
            //一级分类-二级分类-三级分类-显示类型-价格排序类型
            //1-6-26-1-1
            GoodsListUrlParamater result = new GoodsListUrlParamater();
            //校验参数为空
            if (string.IsNullOrEmpty(para)) {
                throw new ArgumentNullException("UrlParamater","UrlParamater 不能为空！");
            }
            string[] all = para.Split('-');
            //校验参数个数
            if (all.Count() != 5) {
                throw new ArgumentException("UrlParamater", "UrlParamater 格式不正确！");
            }
            //校验参数是否都为数字
            int[] allint = new int[all.Count()];
            try {
                for (int i = 0; i <= all.Count() - 1; i++) {
                    allint[i] = Convert.ToInt32(all[i]);
                }
            }
            catch {
                throw new ArgumentException("UrlParamater", "UrlParamater 格式不正确！");
            }
            //分类路径
            result.Path.TopCategoryId = allint[0];
            result.Path.SubCategoryId = allint[1];
            result.Path.ThirdCategoryId = allint[2];
            //显示方式
            switch (allint[3]) {
                case 0: { result.ShowType = GoodsListShowType.Icon; break; }
                case 1: { result.ShowType = GoodsListShowType.List; break; }
            }
            //价格排序
            switch (allint[4]) {
                case 0: { result.PriceSortType = GoodsListPriceSortType.HigherToLower; break; }
                case 1: { result.PriceSortType = GoodsListPriceSortType.LowerToHigher; break; }
            }

            return result;
        }

        public string Compile(GoodsListUrlParamater para) {
            string result = string.Empty;
            result = string.Format("{0}-{1}-{2}-{3}-{4}", para.Path.TopCategoryId, para.Path.SubCategoryId, para.Path.ThirdCategoryId, para.ShowType, para.PriceSortType);
            return result;
        }

        public string Compile(string path) {
            string result = string.Empty;                        
            result = string.Format("{0}-{1}-{2}", path, "0","0");
            return result;
        }
    }
}
